<template>
    <div>  
        <div id="oneLogin" v-if="showOne">
            <h1 v-if="showOne1">欢迎注册pp体验版</h1>
            <span>
                <br><br>
                功能简单，易上手,先去新建一个吧!
            </span>
        </div>
        <!-- 内容 -->
        <div id="content" ref="content" style="display:none;min-height:0px">
            <checklist  
                title="未签到" 
                labelPosition='left'
                :options="objectList1" 
                v-model="objectListValue"
                v-show='this.objectList1.length>0'
            ></checklist>
            <div style="padding:15px;" v-show='this.objectList1.length>0'>
                <x-button 
                    type="primary" 
                    @click.native="saveClick" 
                    :disabled='each1'
                >保存信息</x-button>
            </div>
            <div style="padding:15px;" v-if='each3'>
                <x-button type="primary" @click.native="$vux.toast.text('明天再来');">今日已全部打卡</x-button>
            </div>
            <checklist 
                title="已签到" 
                labelPosition='left'
                :options="objectList2" 
                v-model="objectList2" 
                disabled
                v-show='this.objectList2.length>0'
            ></checklist>
        </div>
    </div>
</template>


<script>
import { Checklist ,XButton ,Divider} from 'vux'
export default {
    data(){
        return {
            showOne:false,   //第一次登录
            showOne1:false,   //没有list
            objectList: [], //所有的列表
            objectList1:[], //未打卡
            objectList2:[], //已打卡
            objectListValue:[],   //dianji选中提交
            
            each1:false,     // 保存成功后再次使用
            each2:true,
            each3:false,
        }
    },
    components:{
        Checklist ,XButton ,Divider
    },
    methods:{
        // 请求列表
        creat(){
            $.get("/daily/day")
            .done(data=>{

                if(data.err==5){
                    this.$vux.toast.text(data.msg);
                    location.href = "#/login";
                    return;
                }
                if(data.data){
                    if(data.data.cData.arr.length===0){
                        // console.log("没有签到项")
                        this.showOne = true;
                        return;
                    }
                    if(data.data.data){
                        this.objectList = data.data.cData.arr;
                        this.objectList2 = [];
                        for(var j=0;j<data.data.data.arr.length;j++){
                            for(var i=0;i<data.data.cData.arr.length;i++){
                                if(data.data.cData.arr[i]===data.data.data.arr[j].list){
                                    this.objectList2.push(data.data.cData.arr[i]);
                                    this.objectList.splice(i,1);
                                    continue;
                                }
                            }
                        }
                      
                        this.objectList1 = this.objectList;
                        this.each3 = this.objectList1.length>0?false:true;
                        
                    }else{
                        // console.log("今天第一次");
                        this.objectList1 = data.data.cData.arr;
                    }
                    $("#content").css('display','none');
                    setTimeout(() => {
                        $("#content").slideToggle(2000);
                    }, 10);
                    
                    
                }else{    
                    // console.log("第一次注册");
                    this.showOne = true;
                    this.showOne1 = true;
                }
            })
            .fail((err)=>{
                this.$vux.toast.text("未知错误，请刷新重试");
            })
        },
        
        // 保存
        saveClick(){
            if(this.objectListValue.length<1){
                this.$vux.toast.text("请先选择打卡");
                return;
            }
            this.each1 = true;
            var arr = JSON.stringify(this.objectListValue);
            $.post("/daily/day",{arr})
            .done(data=>{
                this.objectListValue = [];
                this.each1 = false;
                this.$vux.toast.text(data.msg);
                if(data.err==5){
                    location.href = "#/login";
                }
                if(data.err==0){
                    this.creat();
                }
            })
            .fail((err)=>{
                this.creat();
                this.$vux.toast.text("未知错误，请刷新重试");
            })
        },
        
    },
    beforeCreate:function(){
        // this.$store.dispatch("isLogin");
    },
    activated(){
        this.$emit('title','签 到');
        $("#content").css('display','none');
        this.creat();
    }
}
</script>

<style scoped>
#oneLogin{
    padding: 25px;
    font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
} 
</style>
